package carTest.create.request;

import carTest.create.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 14:40:36 2018/12/14
 * @version:2.0
 */
public class RequestUtil {
    /**
     * 基础类型初始化
     **/
    public static Map<String, String> initType = new HashMap<>();
    /**
     * LIST型初始化
     **/
    public static Map<String, Object> listType = new HashMap<>();
    /**
     * Object型初始化,需要下挖的对象
     **/
    public static Map<String, Object> needDeep = new HashMap<>();

    static {
        initType.put("int", "Integer");
        initType.put("long", "Long");
        initType.put("boolean", "Boolean");
        initType.put("double", "Double");
        initType.put("float", "Float");
    }

    static {
        listType.put("com.alibaba.fastjson.JSONArray", new String());
    }

    static {
        // needDeep.put("int",new Object());
    }

    public static void init(Object o) {
        String jsonString = initStructure(o);// cartest 的 JSON 格式
        jsonString = replaceType(jsonString, "rpc");
        System.out.println(jsonString);
        //不在设定值中的就替换成Object，并下挖
        List<Map> list = JSONObject.parseObject(jsonString, List.class);
        for (Map<String, String> map : list) {
            String dataType = map.get("dataType");
            if (null != dataType) {
                if (dataType.split(":").length == 1) {
                    if (null == RequestLimit.httpLimitMap.get(dataType)) {
                        //默认转换为Object
                        jsonString = jsonString.replaceAll(dataType, "Object");
                        System.out.println(map.get("name") + ":" + dataType + "需要修改格式");
                    }
                } else {

                }

            }
        }
        System.out.println(jsonString);
    }

    public static String initStructure(Object o) {
        List<Map<String, String>> properties = ObjectUtil.getFiledsInfo(o);
        JSONArray arr = new JSONArray();
        String request = "[{\"id\":\"1\",\"parentId\":\"0\",\"level\":\"1\",\"identifier\":\"请求变量1\",\"name\":\"\",\"dataType\":\"String\",\"isMust\":\"是\",\"type\":1}]";
        for (int i = 0; i < properties.size(); i++) {
            JSONObject tmp = new JSONObject();
            String key = properties.get(i).get("name");
            String value = properties.get(i).get("type");
            int index = i + 1;
            tmp.put("id", "" + index + "");
            tmp.put("parentId", "0");
            tmp.put("level", "1");
            tmp.put("identifier", key);
            tmp.put("name", "请求变量1");
            tmp.put("dataType", value);
            tmp.put("isMust", "是");
            tmp.put("type", 1);
            arr.add(tmp);
        }
        return arr.toJSONString();
    }

    public static String initList(String jsonString, String docType) {
        Map<String, String> listMap = new HashMap<>();
        String tmp = "";
        if ("rpc".equals(docType)) {
            tmp = "List";
        }
        if ("http".equals(docType)) {
            tmp = "Array";
        }
        Iterator<String> it = listType.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            listMap.put(key, tmp + ":" + listType.get(key).getClass().toString());
        }
        jsonString = typeReplaceByConfig(jsonString, listMap);
        return jsonString;
    }

    public static String replaceType(String jsonString, String docType) {
        jsonString = initList(jsonString, "rpc");
        jsonString = typeReplaceByConfig(jsonString, initType);
        return jsonString;
    }

    public static String typeReplaceByConfig(String str, Map<String, String> map) {
        str = str.replaceAll("class ", "").replaceAll("java.lang.", "");
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = map.get(key);
            str = str.replaceAll(key, value);
        }
        return str;
    }
}
